#include "ManualCalibrationForm.h"
#include "ui_ManualCalibrationForm.h"
#include "Auxiliary/View/MessageBoxDialog.h"
#include <QDebug>


#define TABLE_VIEW_HEIGHT      (170)
#define TABLE_VIEW_HEAD_HEIGHT (29)

ManualCalibrationForm::ManualCalibrationForm(QWidget *parent):
    ui(new Ui::ManualCalibrationWgt)
{

     ui->setupUi(this);

     ui->gLayout_UserWb->addWidget(&mUserWbCalibView);
     ui->gLayout_UserPredilu->addWidget(&mUserPrediluCalibView);
     ui->gLayout_ProducerWb->addWidget(&mProdWbCalibView);
     ui->gLayout_ProducerPredilu->addWidget(&mProdPrediluCalibView);
     ui->gLayout_TransferWB->addWidget(&mTransferWbCalibView);
     ui->gLayout_TransferPredilu->addWidget(&mTransferPrediluCalibView);

     QStringList userWbCalibTableHead;
     //添加表名
     userWbCalibTableHead<<""<<tr("参数")<<tr("校准系数(%)")<<tr("日期");
     //初始化用户校准视图
     initTableView(mUserWbCalibView);
     //绑定数据库模型表
     initModel(mUserWbCalibViewModel, mUserWbCalibView,"UserWbCalibTable", userWbCalibTableHead);

     QStringList userPrediluCalibTableHead;
     userPrediluCalibTableHead<<""<<tr("参数")<<tr("校准系数(%)")<<tr("日期");
     initTableView(mUserPrediluCalibView);
     initModel(mUserPrediluCalibViewModel,mUserPrediluCalibView,"UserDiluCalibTable", userPrediluCalibTableHead);

     QStringList prodWbCalibTableHead;
     prodWbCalibTableHead<<""<<tr("参数")<<tr("厂家校准系数(%)")<<tr("日期");
     initTableView(mProdWbCalibView);
     initModel(mProdWbCalibViewModel,mProdWbCalibView,"ProducerWbCalibTable", prodWbCalibTableHead);

     QStringList prodPrediluCalibTableHead;
     prodPrediluCalibTableHead<<""<<tr("参数")<<tr("厂家校准系数(%)")<<tr("日期");
     initTableView(mProdPrediluCalibView);
     initModel(mProdPrediluCalibViewModel,mProdPrediluCalibView,"ProducerDiluCalibTable", prodPrediluCalibTableHead);

     QStringList transferWbCalibTableHead;
     transferWbCalibTableHead<<""<<tr("参数")<<tr("传递系数(%)")<<tr("日期");
     initTableView(mTransferWbCalibView);
     initModel(mTransferWbCalibViewModel,mTransferWbCalibView,"TransferWBTable", transferWbCalibTableHead);

     QStringList transferPrediluTableHead;
     transferPrediluTableHead<<""<<tr("参数")<<tr("传递系数(%)")<<tr("日期");
     initTableView(mTransferPrediluCalibView);
     initModel(mTransferPrediluCalibViewModel, mTransferPrediluCalibView, "TransferDiluTable", transferPrediluTableHead);

     mManualCalibration = new ManualCalibration;
}




ManualCalibrationForm::~ManualCalibrationForm()
{
    delete ui;
}

void ManualCalibrationForm::showEvent(QShowEvent *)
{

    mUserWbCalibView.show();
    mUserPrediluCalibView.show();


    // 判断当前登录用户
    loginUser = GlobalManage::instance()->GetLoginUserType();
    //loginUser == Elut_Normaler;
    //loginUser = Elut_Admin;
        loginUser = Elut_Service;
    //loginUser = Elut_Producer;

    if(loginUser == Elut_Normaler)
    {
        mUserWbCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);
        mUserPrediluCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);
        mProdWbCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);
        mProdPrediluCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);
        mTransferWbCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);
        mTransferPrediluCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);

        mProdWbCalibView.hide();
        mProdPrediluCalibView.hide();
        mTransferWbCalibView.hide();
        mTransferPrediluCalibView.hide();


        ui->tabManuCalib->removeTab(1);
        ui->pbtFactoryDefault->hide();
        ui->pbtManuCalibExport->hide();
        ui->pbtManuCalibImport->hide();
        ui->pbtManuCalibSave->hide();
    }
    else if(loginUser == Elut_Admin)
    {
        mUserWbCalibView.setEditTriggers(QAbstractItemView::AllEditTriggers);
        mUserPrediluCalibView.setEditTriggers(QAbstractItemView::AllEditTriggers);
        mProdWbCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);
        mProdPrediluCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);
        mTransferWbCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);
        mTransferPrediluCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);


        mProdWbCalibView.hide();
        mProdPrediluCalibView.hide();
        mTransferWbCalibView.hide();
        mTransferPrediluCalibView.hide();


        ui->tabManuCalib->removeTab(1);
        ui->tabCalibrationCoef->show();
        ui->tabTransferCoef->hide();
        ui->pbtFactoryDefault->hide();
        ui->pbtManuCalibExport->hide();
        ui->pbtManuCalibImport->hide();
        ui->pbtManuCalibSave->hide();

    }
    else if(loginUser == Elut_Service)
    {
        mUserWbCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);
        mUserPrediluCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);
        mProdWbCalibView.setEditTriggers(QAbstractItemView::AllEditTriggers);
        mProdPrediluCalibView.setEditTriggers(QAbstractItemView::AllEditTriggers);
        mTransferWbCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);
        mTransferPrediluCalibView.setEditTriggers(QAbstractItemView::NoEditTriggers);


        mProdWbCalibView.show();
        mProdPrediluCalibView.show();
        mTransferWbCalibView.show();
        mTransferPrediluCalibView.show();



        ui->tabManuCalib->addTab(ui->tabTransferCoef, tr("传递系数"));

        ui->tabCalibrationCoef->show();
        ui->tabTransferCoef->show();
        ui->pbtFactoryDefault->show();
        ui->pbtManuCalibExport->show();
        ui->pbtManuCalibImport->show();
        ui->pbtManuCalibSave->show();

    }
    else if(loginUser ==  Elut_Producer || loginUser ==  Elut_Developer)
    {
        mUserWbCalibView.setEditTriggers(QAbstractItemView::AllEditTriggers);
        mUserPrediluCalibView.setEditTriggers(QAbstractItemView::AllEditTriggers);
        mProdWbCalibView.setEditTriggers(QAbstractItemView::AllEditTriggers);
        mProdPrediluCalibView.setEditTriggers(QAbstractItemView::AllEditTriggers);
        mTransferWbCalibView.setEditTriggers(QAbstractItemView::AllEditTriggers);
        mTransferPrediluCalibView.setEditTriggers(QAbstractItemView::AllEditTriggers);

        mProdWbCalibView.show();
        mProdPrediluCalibView.show();
        mTransferWbCalibView.show();
        mTransferPrediluCalibView.show();

        ui->tabManuCalib->addTab(ui->tabTransferCoef, tr("传递系数"));
        ui->tabCalibrationCoef->show();
        ui->tabTransferCoef->show();
        ui->pbtFactoryDefault->show();
        ui->pbtManuCalibExport->show();
        ui->pbtManuCalibImport->show();
        ui->pbtManuCalibSave->show();

    }

}

void ManualCalibrationForm::closeEvent(QCloseEvent *)
{

    MessageBoxDialog *messageBox = new MessageBoxDialog(this,\
                             QString(tr("是否保存？")));
    int result = messageBox->exec();
    if(result == QDialog::Accepted)
    {
       mProdWbCalibViewModel.submitAll();
    }
    else
    {


    }




}

void ManualCalibrationForm::initTableView(QTableView &view)
{
   view.setFrameShape(QFrame::NoFrame);
   view.setEditTriggers(QAbstractItemView::AllEditTriggers);
   view.verticalHeader()->setResizeMode(QHeaderView::Fixed);
   view.horizontalHeader()->setResizeMode(QHeaderView::Fixed);
   view.horizontalHeader()->setFixedHeight(TABLE_VIEW_HEAD_HEIGHT);
   view.horizontalHeader()->setStyleSheet("QHeaderView::section{background:rgb(96,204,152);}");
   view.setSelectionMode(QAbstractItemView::NoSelection);
   view.verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
   view.verticalHeader()->hide();


   //隐藏滑动条
   view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

   view.setAlternatingRowColors(true);//间隔显示颜色

   QString tableStyle = "QTableCornerButton::section{background:rgb(102, 204, 153);border:1px solid#c9c9c9;}"
           "QHeaderView::section{background:rgb(102, 204, 153);border:1px solid#c9c9c9;color:black;font:bold}"
           "QTableView{background-color:#e2f7eb;"
           "alternate-background-color:#ffffff;"
           "color: rgb(0, 0, 0);"
           "selection-color: rgb(255, 255, 255);"
           "selection-background-color: rgb(32, 131, 95);"
           "}";
   view.setStyleSheet(tableStyle);

   //delegate = new LineEditDelegate(this);
   delegate = new ManualCalibViewItemDelegate(this);
   QString conditions = "\\d{0,3}(\\.\\d{1,2})?";
   delegate->getlineeditconditions(conditions);

   view.setItemDelegateForColumn(2, delegate);
}

void ManualCalibrationForm::initModel(ManualCalibViewModel &model, QTableView &view, QString tableName, QStringList headlist)
{
    //设置数据库表
    model.setTable(tableName);
    model.select();
    model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    //绑定视图
    view.setModel(&model);
    //插入表头
    for(int i = 0; i < headlist.size(); i++)
    {
        model.setHeaderData(i, Qt::Horizontal, headlist.at(i));
    }
    //隐藏ID序号列
    view.setColumnHidden(0, true);
    view.setColumnWidth(1, 67);
    view.setColumnWidth(2, 130);
    view.setColumnWidth(3, 106);

    int rowNum = model.rowCount();
    for(int i = 0; i < rowNum; i++)
    {
        view.setRowHeight(i, (TABLE_VIEW_HEIGHT - TABLE_VIEW_HEAD_HEIGHT)/rowNum);
    }
}

void ManualCalibrationForm::init()
{

}

void ManualCalibrationForm::unInit()
{

}

void ManualCalibrationForm::closed()
{

}

void ManualCalibrationForm::on_pbtFactoryDefault_clicked()
{
    MessageBoxDialog *messageBox = new MessageBoxDialog(this,\
                             QString(tr("恢复出厂值会同时初始化出厂校准系数和用户校准系数，\n是否继续？")));
    int result = messageBox->exec();
    if(result == QDialog::Accepted)
    {
        mManualCalibration->factoryDefault(mUserWbCalibViewModel);
        mManualCalibration->factoryDefault(mUserPrediluCalibViewModel);
        mManualCalibration->factoryDefault(mProdWbCalibViewModel);
        mManualCalibration->factoryDefault(mProdPrediluCalibViewModel);
        mManualCalibration->factoryDefault(mTransferWbCalibViewModel);
        mManualCalibration->factoryDefault(mTransferPrediluCalibViewModel);
    }
    else{



    }
}

void ManualCalibrationForm::on_pbtManuCalibExport_clicked()
{

}

void ManualCalibrationForm::on_pbtManuCalibImport_clicked()
{

}

void ManualCalibrationForm::on_pbtManuCalibSave_clicked()
{

}


